package cn.com.lms.business.excel;


import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.apache.poi.xssf.streaming.SXSSFRow;
import org.apache.poi.xssf.streaming.SXSSFSheet;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Map;

/**
 * @ClassName ExportThread
 * @Description: 导出线程
 * @Author zhangzhenjiang
 * @Date 2020-1-8 11:41
 * @Version V1.0
 **/
public class ExportThread extends Thread {
    /**
     * sheet实体
     */
    private SXSSFSheet sheet;
    /**
     * 行数
     */
    private Integer rows;

    /**
     * 行对应的列的数据
     */
    private Map<Integer, List<String>> rowMap;

    private CellStyle cellStyle;


    public ExportThread(SXSSFSheet sheet, Integer rows, Map<Integer, List<String>> rowMap, CellStyle cellStyle) {
        // 居中
        cellStyle.setAlignment(HorizontalAlignment.CENTER);
        cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
        this.sheet = sheet;
        this.rows = rows;
        this.rowMap = rowMap;
        this.cellStyle = cellStyle;

    }

    @Override
    public void run() {
        for (int i = 0; i <= this.rows; i++) {
            int row=i;
            SXSSFRow sxssfRow = this.sheet.createRow(i);
             List<String> cellList = rowMap.get(i);
            if (!CollectionUtils.isEmpty(cellList)) {
                for(int j=0;j<cellList.size();j++){
                    sxssfRow.createCell(j).setCellValue(cellList.get(j));
                    this.sheet.setDefaultColumnStyle(j, this.cellStyle);
                }

            }
        }
    }
}
